home *** CD-ROM | disk | FTP | other *** search
- #ifndef lint
- static char rcsid[] = "$Header: xinput.c,v 1.8 87/05/19 18:46:30 schoch Exp $";
- #endif
-
- #include "externs.h"
- #include <ctype.h>
- #include <strings.h>
- #include <sys/signal.h>
-
- bool note_on;
- bool isiconic;
-
- handle_input()
- {
- XEvent event;
- XKeyOrButtonEvent *ke;
- static x = -1, y = -1;
- static pos = -1;
- char *cp;
- auto int n;
-
- XNextEvent(&event);
- switch (event.type) {
- case ExposeWindow:
- case ExposeRegion:
- if (event.window == iconwindow) {
- redraw_icon(color == ourcolor);
- isiconic = TRUE;
- } else {
- if (event.type == ExposeRegion) {
- XExposeRegionEvent *xe = (XExposeRegionEvent *)&event;
-
- redraw_region(xe->x, xe->y, xe->width, xe->height);
- } else
- redraw();
- isiconic = FALSE;
- }
- break;
-
- case ButtonPressed:
- ke = (XKeyOrButtonEvent *)&event;
- switch(ke->detail & 0xff) {
- case RightButton:
- break;
- case MiddleButton:
- break;
- case LeftButton:
- x = ke->x; y = ke->y;
- pos = xytopos(x, y);
- startmove(pos, x, y);
- break;
- default:
- printf("Unknown Button pressed.\n");
- break;
- }
- break;
- case ButtonReleased:
- ke = (XKeyOrButtonEvent *)&event;
- switch(ke->detail & 0xff) {
- case LeftButton:
- if (move)
- stopmove(pos, x, y);
- break;
- }
- break;
-
- case LeaveWindow:
- if (pos == -1)
- break;
- pos = -1;
- if (move)
- stopmove(pos, x, y);
- x = -1;
- y = -1;
- break;
-
- case MouseMoved:
- ke = (XKeyOrButtonEvent *)&event;
- pos = xytopos(ke->x, ke->y);
- if (move)
- piecemove(x, y, ke->x, ke->y);
- x = ke->x; y = ke->y;
- break;
-
- case KeyPressed:
- ke = (XKeyOrButtonEvent *)&event;
- cp = XLookupMapping(ke, &n);
- key_func(cp, n);
- break;
-
- default:
- fprintf(stderr, "Unknown event type %d\n", event.type);
- break;
- }
- }
-
- handle_sock()
- {
- char instr[BUFSIZ];
- char *cp;
- int pstart, pend, i;
-
- if (fgets(instr, sizeof instr, inp) == NULL) {
- message("Connection closed.", MESSAGE);
- dead = 1;
- state = OVER;
- return 1;
- }
- if (cp = index(instr, '\n'))
- *cp = '\0';
- if (cp = index(instr, '\r'))
- *cp = '\0';
- i = parse_move(&pstart, &pend, theircolor, instr);
- if (i < 0) /* Message */
- return 0;
- if (i != TRUE)
- pstart = pend = 0;
- if (movetry(pstart, pend, theircolor) == 0) {
- XFeep(0);
- if (isiconic)
- flash_icon();
- }
- return 0;
- }
-
- /* Takes window coordinates and returns a board position from 0-99.
- * returns -1 if point is not on board. */
- xytopos(x, y)
- {
- x += 64; y += 64-TOPSPACE;
- if (y < 0 || x < 0)
- return -1;
- x /= 64;
- y /= 64;
- if (x > 9 || y > 9)
- return -1;
- if (reverse) {
- y = 9 - y;
- x = 9 - x;
- }
- return (y * 10) + x;
- }
-
- parse_move(pstart, pend, color, instr)
- int *pstart, *pend;
- u_char color;
- char *instr;
- {
- char buf[BUFSIZ];
- extern pawntries; /* in xmovetry.c */
-
- if (!strcmp (instr, "draw")) {
- drawok [color] = TRUE;
- return FALSE;
- }
- if (!strcmp (instr, "resign")) {
- resign = TRUE;
- return FALSE;
- }
- if (drawok [1 - color]) {
- if (!strcmp (instr, "yes")) {
- drawok [color] = TRUE;
- } else if (!strcmp (instr, "no")) {
- ;
- } else {
- printf("error: should get yes or no.\n");
- }
- return FALSE;
- }
- if (!strncmp (instr, "say", 3)) {
- sprintf(buf, "%s:%s", colorname[color], instr+3);
- if (note_on)
- message(buf, CHECK);
- else
- message(buf, MESSAGE);
- return -1;
- }
- if ((!option [ANNOUNCEPAWNS])
- && (!strcmp (instr, "any"))) {
- if (pawntries)
- message ("pawntries", PAWNTRIES);
- else
- message ("no pawntries", PAWNTRIES);
- return -1;
- }
- if (parse_algebraic_move(pstart,pend,instr) == TRUE)
- return TRUE;
-
- printf("Invalid input: %s\n", instr);
- return FALSE;
- }
-
- #define boardpos(col, row) ((9 - (row)) * 10 + (col))
- #define RIGHT_SIDE 2
- #define RIGHT_FILE 1
-
- /* parse an algebraic move (e2-e4), and return TRUE for non error */
- parse_algebraic_move (pstart, pend, instr)
- int *pstart, *pend;
- char *instr;
- {
-
- if (instr[2] != '-')
- return FALSE;
- if (instr [0] >= 'a' && instr [0] <= 'h'
- && instr [1] >= '1' && instr [1] <= '8'
- && instr [3] >= 'a' && instr [3] <= 'h'
- && instr [4] >= '1' && instr [4] <= '8') {
- *pstart = boardpos (instr[0]-'a'+1, instr[1]-'0');
- *pend = boardpos (instr[3]-'a'+1, instr[4]-'0');
- return TRUE;
- } else
- return FALSE;
- }
-
- key_func(s, n)
- char *s;
- {
- if (n == 0)
- return;
- if (note_on) {
- if (do_note(s, n))
- note_on = 0;
- return;
- }
- switch(*s) {
- case 'r':
- case 'R':
- replay();
- break;
- case 's':
- case 'S':
- stop_replay();
- break;
- case 'd':
- case 'D':
- draw();
- break;
- case 'q':
- case 'Q':
- do_resign();
- break;
- case 'y':
- case 'Y':
- yes();
- break;
- case 'n':
- case 'N':
- no();
- break;
- case 'm':
- case 'M':
- send_note(s, n);
- break;
- case 'x':
- case 'X':
- xit();
- break;
- case 'a':
- case 'A':
- any();
- break;
- case '?':
- case 'h':
- case 'H':
- help();
- break;
- }
- }
-
- send_note(s, n)
- char *s;
- {
- if (dead) {
- message("Your opponent is gone.", MESSAGE);
- return;
- }
- note_on = TRUE;
- s++; n--;
- start_note();
- if (n > 0)
- if (do_note(s, n))
- note_on = 0;
- }
-
- draw()
- {
- if (state != PLAYING) {
- message("The game is over!", MESSAGE);
- return;
- }
- if (color != ourcolor) {
- message("Wait for your turn.", MESSAGE);
- return;
- }
- drawok[ourcolor] = TRUE;
- fputs("draw\r\n", out);
- movetry(0, 0, ourcolor);
- }
-
- yes()
- {
- if (state != PLAYING) {
- message("The game is over!", MESSAGE);
- return;
- }
- if (drawok[theircolor] != TRUE)
- return;
- drawok[ourcolor] = TRUE;
- fputs("yes\r\n", out);
- movetry(0, 0, ourcolor);
- }
-
- no()
- {
- if (state != PLAYING) {
- message("The game is over!", MESSAGE);
- return;
- }
- if (drawok[theircolor] != TRUE)
- return;
- drawok[ourcolor] = FALSE;
- fputs("no\r\n", out);
- movetry(0, 0, ourcolor);
- }
-
- do_resign()
- {
- if (state != PLAYING) {
- message("The game is over!", MESSAGE);
- return;
- }
- fputs("resign\r\n", out);
- resign = TRUE;
- movetry(0, 0, ourcolor);
- }
-
- xit()
- {
- if (state == PLAYING) {
- message("Type Q (resign) or D (draw) first.", MESSAGE);
- return;
- }
- exit(0);
- }
-
- help()
- {
- if (state == REVIEW)
- message("Type S (stop review), X (exit), or M (message)", MESSAGE);
- else if (state == OVER)
- message("Type R (review), or X (exit), or M (message)", MESSAGE);
- else if (drawok[theircolor] && !drawok[ourcolor])
- message("Type Y (accept draw), N (no), or M (message)", MESSAGE);
- else
- message("Type Q (resign), D (draw), or M (message)", MESSAGE);
- }
-
- replay()
- {
- if (state == PLAYING) {
- message("You have to resign first!", MESSAGE);
- } else
- review();
- }
-
- any()
- {
- if (state != PLAYING) {
- message("The game is over!", MESSAGE);
- return;
- }
- if (color != ourcolor) {
- message("It's not your turn.", MESSAGE);
- return;
- }
- if (option[ANNOUNCEPAWNS]) {
- message("The \"announce pawntries\" option is on!", MESSAGE);
- return;
- }
- if (pawntries)
- message ("pawntries", PAWNTRIES);
- else
- message ("no pawntries", PAWNTRIES);
- fputs("any\r\n", out);
- }
-
- stop_replay()
- {
- if (state == REVIEW)
- state = OVER;
- }
-
- flash_icon()
- {
- onalrm(SIGALRM);
- }
-
- onalrm(sig)
- {
- static reverse = FALSE;
-
- if (color != ourcolor || !isiconic) {
- reverse = FALSE;
- return;
- }
- signal(sig, onalrm);
- reverse = !reverse;
- redraw_icon(reverse);
- alarm(1);
- }
-